package com.zk.erp.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk.erp.entity.JsonResult;
import com.zk.erp.entity.Role;
import com.zk.erp.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zk
 * @since 2021-04-12
 */
@RestController
public class RoleController {
@Autowired
    private IRoleService roleService;
    //分页查询role
    @RequestMapping(value = "/roles",method = RequestMethod.GET)
    public JsonResult findPage(@RequestParam(value = "curr",required = false,defaultValue = "1")Long curr){
        Page<Role> rolePage = new Page<>(curr,10);
        //IPage<Role> page = roleService.findPage(rolePage);
        IPage<Role> page = roleService.page(rolePage, null);
        return new JsonResult(1,page);
    }
    //根据userId查roles
    @RequestMapping(value = "/roles/user/{userId}",method = RequestMethod.GET)
    public JsonResult findRolesByUserId(@PathVariable("userId")String userId){
        List<String> roleByUserId = roleService.findRoleIdsByUserId(userId);
        return new JsonResult(1,roleByUserId);
    }
    //查询所有的roles
    @RequestMapping(value = "/roles",method = RequestMethod.POST)
    public JsonResult findRoles(){
        List<Role> list = roleService.list();
        return new JsonResult(1,list);
    }

    //修改role
    @RequestMapping(value = "/role",method = RequestMethod.PUT)
    public JsonResult modify(@RequestBody Role role,@RequestParam(value = "curr")String curr){
        roleService.updateById(role);
        return new JsonResult(1,curr);
    }

    //删除role
    @RequestMapping(value = "/role/{roleId}",method = RequestMethod.DELETE)
    public JsonResult deleteRole(@PathVariable("roleId")String roleId,String curr){
        roleService.removeById(roleId);
        return new JsonResult(1,curr);
    }
}
